import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { DeviceMAccess } from "@/api/backend/index";
import { ElNotification } from "element-plus"; //处理导航信息
import { log } from "console";

export const useAccess = defineStore(Names.access, {
  state: () => {
    return {
      defaultFilter: { pageNum: 1, pageSize: 10 },
      item: {}, //设备接入详情
      single: {
        brand: null,
        code: "408",
        createBy: "admin",
        createTime: "2023-10-26 16:16:01",
        equipmentCode: "DZ-SCSB-AII-204",
        equipmentName: "滚丝机MC-10",
        equipmentType: "滚丝机",
        gatewaycode: null,
        id: 3,
        name: "滚丝车间",
        orderNum: null,
        parentId: null,
        picture: null,
        remark: null,
        runTime: null,
        state: null,
        typeId: null,
        updateBy: null,
        updateTime: null,
      },
      switch: false, //设备启停功能或显示
      companyid: null, //用户的企业id
      gatewayShow: false, //绑定网关弹出框
      internetNumShow: false, //绑定流量卡号弹出框
      model: "", //弹出框类型
    };
  },
  getters: {},
  actions: {
    //默认获取设备列表
    getAccessList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(DeviceMAccess + "/listBytree", filter, "json", "")
          .then((res) => {
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //查询设备列表接口 通常由其他模块调用数据
    getDevicesListByOther(params: any) {
      let filterData = Object.assign(this.defaultFilter, params);
      return new Promise((resolve, reject) => {
        _get(DeviceMAccess + "/list", filterData, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //查询单个设备详情
    getDevicesItem(params: any) {
      return new Promise((resolve, reject) => {
        _get(DeviceMAccess + `/${params}`, {}, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    getDetail(id: any) {
      return new Promise((resolve, reject) => {
        _get("/system/estate/list", { eid: id }, "json", "")
          .then((res) => {
            resolve(res as any);
          })
          .catch((err) => {
            ElNotification({
              type: "error",
              message: "初始化过程出错，请联系管理人员",
            });
          });
      });
    },
    //更新单个设备数据
    upDateDevice(data: any) {
      console.log("更新单个设备数据", data);
      return new Promise((resolve, reject) => {
        _put("/system/equipment", data, "json", "")
          .then((res) => {
            resolve(res as any);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //网关绑定
    postGatewayBinding(data: anyObj) {
      console.log("开始绑定网关", data);
      return new Promise((resolve, reject) => {
        _put("/system/equipment", data, "json", "")
          .then((res) => {
            resolve(res as any);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //流量卡绑定
    postInternetBinding(data: anyObj) {
      console.log("开始绑定网关", data);
      return new Promise((resolve, reject) => {
        _put("/system/equipment", data, "json", "")
          .then((res) => {
            resolve(res as any);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //实况页面获取设备信息 eid内容为网关编号
    getDeviceByGateway(ecode: string, regions: any[]) {
      return new Promise((resolve, reject) => {
        _get(
          "system/equipment/listBytree",
          { code: ecode, regions: regions + "" },
          "json",
          ""
        )
          .then((res) => {
            resolve(res as any);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
  },
});
